<template>
  <div class="login-container">
    <div class="login-box">
      <h2>欢迎回来，零食爱好者！</h2>
      <form @submit.prevent="handleLogin">
        <div class="input-group">
          <label for="username">用户名：</label>
          <input type="text" id="username" v-model="username" required placeholder="请输入您的用户名">
        </div>
        <div class="input-group">
          <label for="password">密码：</label>
          <input type="password" id="password" v-model="password" required placeholder="请输入您的密码">
        </div>
        <button type="submit" class="submit-btn">开启美味之旅</button>
      </form>
      <p class="register-link">还没有账号？<router-link to="/register">立即注册</router-link></p>
    </div>
  </div>
</template>
<script setup>
import { ref } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { useUserStore } from '../stores/userStore';
import { ElMessage } from 'element-plus';

const router = useRouter();
const route = useRoute();
const userStore = useUserStore();
const username = ref('');
const password = ref('');

const handleLogin = async () => {
  try {
    // 这里应该调用您的登录 API
    // const response = await api.login(username.value, password.value);
    
    // 模拟登录成功
    localStorage.setItem('token', 'dummy_token');
    userStore.login({
      username: username.value,
      // 其他用户信息...
    });
    
    ElMessage.success('登录成功');
    
    // 获取重定向地址
    const redirectPath = route.query.redirect || '/profile';
    router.push(redirectPath);
  } catch (error) {
    ElMessage.error('登录失败，请检查用户名和密码');
  }
};
</script>
<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #FFF3E0; /* 淡橙色背景 */
}
.login-box {
  background-color: white;
  padding: 30px;
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  width: 300px;
}
h2 {
  color: #FF6F00; /* 深橙色 */
  text-align: center;
  margin-bottom: 20px;
}
.input-group {
  margin-bottom: 15px;
}
label {
  display: block;
  margin-bottom: 5px;
  color: #FF6F00;
}
input {
  width: 100%;
  padding: 10px;
  border: 1px solid #FFB74D; /* 浅橙色边框 */
  border-radius: 5px;
}
.submit-btn {
  width: 100%;
  padding: 10px;
  background-color: #FF6F00;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s;
}
.submit-btn:hover {
  background-color: #F57C00;
}
.register-link {
  text-align: center;
  margin-top: 15px;
}
a {
  color: #FF6F00;
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}
</style>
